package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmUpdateDataService;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.PaasProcessDataEntity;
import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.domain.entity.PaasUserEntity;
import com.xbongbong.paas.domain.entity.StageProcessEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.StageTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.PaasFormHelp;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.help.UserHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.log.thread.LogThreadPoolBeanConfig;
import com.xbongbong.paas.model.PaasStageModel;
import com.xbongbong.paas.model.PaasUserModel;
import com.xbongbong.paas.model.StageProcessModel;
import com.xbongbong.paas.pojo.FormTitlePoJo;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateVO;
import com.xbongbong.paas.service.DetailStageService;
import com.xbongbong.paas.service.PaasFormDataService;
import com.xbongbong.paas.service.ProModelService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.paas.util.TitleUtil;
import com.xbongbong.parent.factory.StageExecuteFactory;
import com.xbongbong.parent.interfaces.StageExecuteHandle;
import com.xbongbong.pro.businesstage.pojo.dto.CommonJumpDTO;
import com.xbongbong.pro.domain.entity.ReRollBackEntity;
import com.xbongbong.pro.enums.PaasStageEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.AfterSavePojo;
import com.xbongbong.pro.formdata.pojo.vo.BeforeUpdateVO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.PaasFormDataModel;
import com.xbongbong.paas.data.service.RichTextService;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.workflow.domain.entity.WorkflowBackupDataEntity;
import com.xbongbong.workflow.model.WorkflowBackupDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/02/23 16:03
 */
@Service("paasDataFormDataUpdateServiceImpl")
public class PaasDataFormDataUpdateServiceImpl extends AbstractCrmUpdateDataService {

    private static final Logger LOG = LoggerFactory.getLogger(PaasDataFormDataUpdateServiceImpl.class);

    @Resource
    private UserHelp userHelp;
    @Resource
    private ProModelService proModelService;
    @Resource
    private PaasFormDataModel paasFormDataModel;
    @Resource
    private PaasFormHelp paasFormHelp;

    @Resource
    private LogThreadPoolBeanConfig logThreadPoolBeanConfig;
    @Resource
    private WorkflowBackupDataModel workflowBackupDataModel;
    @Resource
    private RichTextService richTextService;
    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private DetailStageService detailStageService;
    @Resource
    private StageProcessModel stageProcessModel;
    @Resource
    private PaasStageModel paasStageModel;
    @Resource
    private StageExecuteFactory stageExecuteFactory;
    @Resource
    private PaasFormDataService paasFormDataService;
    @Resource
    private PaasUserModel paasUserModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.PAAS.getCode());
    }

    @Override
    public void afterSaveLog(FormDataUpdateDTO formDataUpdateDTO, PaasFormEntityExt paasFormEntityExt, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity, UpdateVO updateVO, BeforeUpdateVO beforeUpdateVO) throws XbbException {
        RedundantTemplateTypeEnum redundantTemplateTypeEnum =  RedundantTemplateTypeEnum.PAAS;
        boolean isCoverage = Objects.equals(1, formDataUpdateDTO.getIsCover());
        if(isCoverage){
           return;
        }
        // 记录日志
        List<FieldAttrEntity> list = beforeUpdateVO.getExplainList();
        Map<String, FieldAttrEntity> explainMap = beforeUpdateVO.getExplainMap();
        PaasFormDataEntityExt paasFormDataEntityExt = new PaasFormDataEntityExt();
        BeanUtil.copyProperties(paasFormDataEntity, paasFormDataEntityExt);
        Runnable runnable = () -> {
            try {
                List<FormTitlePoJo> formTitleList = ExplainUtil.getFormTitle(JSONArray.parseArray(paasFormEntityExt.getCustomTitle()), explainMap, paasFormDataEntity.getData(), list, paasFormDataEntityExt);
                String title = "--";
                if(!formTitleList.isEmpty()){
                    formTitleList.forEach(titlePoJo->{
                        FieldAttrEntity fieldAttrEntity = explainMap.get(titlePoJo.getAttr());
                        if (Objects.nonNull(fieldAttrEntity) && ExplainUtil.isCombo(fieldAttrEntity.getFieldType())) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put(titlePoJo.getAttr(), titlePoJo.getValue());
                            Object value = ItemUtil.analysisComboReturnStr(jsonObject, fieldAttrEntity);
                            titlePoJo.setValue(Collections.singletonList(value));
                        }
                    });
                    title = TitleUtil.getTitle(formTitleList);
                }
                if(StringUtil.isEmpty(title)){
                    title = "--";
                }

                String opObjectName = title;
                String memoName = beforeUpdateVO.getPaasFormEntityExt().getName();
                String userName = updateVO.getUserVO().getName();
                String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_WORK), userName, memoName, opObjectName);

                saveLogger(formDataUpdateDTO, paasFormDataEntity, redundantTemplateTypeEnum, memo, opObjectName, OperateModuleTypeEnum.PAAS_FORM_DATA, updateVO, beforeUpdateVO,
                        OperateTypeEnum.EDIT, IndexTypeEnum.IDX_SUB_FORM_DATA);
            } catch (Exception e) {
                LOG.error("com.xbongbong.paas.service.impl.paasDataFormDataUpdateServiceImpl.afterSaveLog fail", e);
            }
        };

        logThreadPoolBeanConfig.sysLogThreadPool().execute(runnable);
    }

    @Override
    public UpdateVO update(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, PaasFormEntityExt paasFormEntity, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        try {
            String corpid = formDataUpdateDTO.getCorpid();
            UserVO userVO = userHelp.getUserName(formDataUpdateDTO.getCorpid(), formDataUpdateDTO.getUserId(), formDataUpdateDTO.getLoginUserName(), beforeUpdateVO.getPaasFormDataEntity().getFlowStatus(), beforeUpdateVO.getPaasFormDataEntity().getCreatorId());
            Integer noApprovalRequired = beforeUpdateVO.getNoApprovalRequired();
            Integer updateLine = 0;
            if ( Objects.equals(noApprovalRequired, 1) ) {
                FormDataUtil.removeSystemData(paasFormDataEntity.getData());
                Object richTextData = removeRichText(paasFormDataEntity.getData());
                updateLine = paasFormDataModel.update(paasFormDataEntity);


                //文件柜数据保存
                saveFile(formDataUpdateDTO, paasFormDataEntity, userVO);
                //富文本数据更新
                updateFormRichText(formDataUpdateDTO, richTextData);
            }
            return new UpdateVO(noApprovalRequired, paasFormDataEntity, updateLine, formDataUpdateDTO.getDataId(), userVO);
        } catch (XbbException e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            LOG.error("purchaseInvoiceFormDataUpdateServiceImpl.update 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }


    @Override
    public FormDataUpdateVO afterSave(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, UpdateVO updateVO, PaasFormEntityExt paasFormEntity, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        String corpid = formDataUpdateDTO.getCorpid();
        Long dataId = formDataUpdateDTO.getDataId();
        Integer businessType = formDataUpdateDTO.getBusinessType();
        try {
            FormDataUpdateVO formDataUpdateVO = new FormDataUpdateVO();
            Integer noApprovalRequired = updateVO.getNoApprovalRequired();
            if (Objects.equals(1, noApprovalRequired)) {
                SaasFormSaveDTO saasFormSaveDTO = new SaasFormSaveDTO();
                BeanUtil.copyProperties(formDataUpdateDTO, saasFormSaveDTO);
                packageSaasFormUpdateSaveDTO(saasFormSaveDTO, paasFormDataEntity, oldPaasFormDataEntity);
                // 保存表单后的操作
                AfterSavePojo afterSavePojo = paasFormDataService.afterSave(saasFormSaveDTO);
                // 日志
                afterSaveLog(formDataUpdateDTO, paasFormEntity, paasFormDataEntity, oldPaasFormDataEntity, updateVO, beforeUpdateVO);
                // api发起
                apiHook(formDataUpdateDTO, updateVO, beforeUpdateVO);

                //阶段处理
                afterSaveStage(formDataUpdateDTO, beforeUpdateVO);
                // 2021/3/3 业务规则
                activeRule(formDataUpdateDTO, beforeUpdateVO, updateVO);
                formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_RETURN_MSG));
                formDataUpdateVO.setCode(PaasConstant.FORM_ADD_RETURN_CODE);
            } else if (Objects.equals(noApprovalRequired, 2)) {
                // 自动拒绝
                formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_PROCESS_RETURN_MSG));
                formDataUpdateVO.setCode(PaasConstant.FORM_ADD_PROCESS_RETURN_CODE);
            } else {
                formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_PROCESS_RETURN_MSG));
                formDataUpdateVO.setCode(PaasConstant.FORM_ADD_PROCESS_RETURN_CODE);
                PaasProcessDataEntity paasProcessDataEntity = new PaasProcessDataEntity();
                paasProcessDataEntity.setCorpid(formDataUpdateDTO.getCorpid());
                paasProcessDataEntity.setFormDataId(dataId);
                paasProcessDataEntity.setBusinessType(businessType);
                paasProcessDataEntity.setSaasMark(formDataUpdateDTO.getSaasMark());
                proModelService.updateFormDataStatus(paasProcessDataEntity, FlowStatusEnum.IN_APPROVAL.getType());
            }
            formDataUpdateVO.setFormDataId(dataId);
            return formDataUpdateVO;
        } catch (XbbException e) {
            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, new ReRollBackEntity());
            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, new ReRollBackEntity());
            LOG.error("purchaseInvoiceFormDataUpdateServiceImpl.afterSave 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    private void afterSaveStage(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO) throws XbbException {
        boolean hasStageField = false;
        boolean isFromProcess = Objects.equals(formDataUpdateDTO.getIsFromProcess(), BasicConstant.ONE);
        String corpid = formDataUpdateDTO.getCorpid();
        JSONObject newData = beforeUpdateVO.getPaasFormDataEntity().getData();
        JSONObject oldData = beforeUpdateVO.getOldPaasFormData().getData();
        Integer businessType = formDataUpdateDTO.getBusinessType();
        PaasFormDataEntity newPaasFormDataEntity = beforeUpdateVO.getPaasFormDataEntity();
        Long dataId = newPaasFormDataEntity.getId();
        Long formId = newPaasFormDataEntity.getFormId();
        PaasFormExplainEntity explainEntity = proFormHelp.getExplainEntity(corpid, formId, businessType);
        List<FieldAttrEntity> fieldAttrEntities = JSON.parseArray(explainEntity.getExplains(), FieldAttrEntity.class);

        String stageAttr = "";
        for (FieldAttrEntity fieldAttrEntity : fieldAttrEntities) {
            if (Objects.equals(FieldTypeEnum.STAGE_THRUSTER.getType(), fieldAttrEntity.getFieldType()) && Objects.equals(BasicConstant.ONE, fieldAttrEntity.getIsOpen())){
                hasStageField = true;
                stageAttr = fieldAttrEntity.getAttr();
                break;
            }
        }
        //判断有没有阶段推进器字段
        if (hasStageField) {
            Long stageProcessId = FastJsonHelper.getLongOrDefaultFromFormData(newData, PaasStageEnum.STAGE_PROCESS_ID.getAttr(), BasicConstant.ZERO_LONG);
            StageProcessEntity stageProcessEntity = stageProcessModel.getByKey(stageProcessId, corpid);
            Long toStageId = FastJsonHelper.getLongOrDefaultFromFormData(newData, PaasStageEnum.STAGE_ID.getAttr(), BasicConstant.ZERO_LONG);
            Long fromStageId = FastJsonHelper.getLongOrDefaultFromFormData(oldData, PaasStageEnum.STAGE_ID.getAttr(), BasicConstant.ZERO_LONG);
            PaasStageEntity toStageEntity = paasStageModel.getByKey(toStageId, corpid);
            PaasStageEntity fromStageEntity = paasStageModel.getByKey(fromStageId, corpid);
            //判断阶段是否发生阶段改变,发生阶段改变才需要跳转
            boolean isChangeStage = !Objects.equals(toStageId, BasicConstant.ZERO_LONG) && !Objects.equals(toStageId, fromStageId);
            if (isChangeStage) {
                try {
                    StageExecuteHandle stageExecute = stageExecuteFactory.getStageExecute(SaasMarkEnum.PAAS.getCode(), businessType);
                    //封装阶段跳转参数
                    CommonJumpDTO commonJumpDTO = new CommonJumpDTO();
                    // 从BaseDTO拷贝loginUser等基础信息
//                    BeanUtil.copyProperties(formDataUpdateDTO, commonJumpDTO);
                    commonJumpDTO.setPlatform(formDataUpdateDTO.getPlatform());
                    commonJumpDTO.setSaasMark(SaasMarkEnum.PAAS.getCode());
                    commonJumpDTO.setBusinessType(businessType);
                    commonJumpDTO.setFormId(formId);
                    commonJumpDTO.setDataId(dataId);
                    commonJumpDTO.setCorpid(corpid);
                    commonJumpDTO.setUserId(isFromProcess ? formDataUpdateDTO.getProcessCreatorId() : formDataUpdateDTO.getUserId());
                    commonJumpDTO.setFromStageType(Objects.isNull(fromStageEntity) ? StageTypeEnum.START_STAGE.getType() : fromStageEntity.getType());
                    commonJumpDTO.setToStageType(toStageEntity.getType());
                    commonJumpDTO.setFromStageEntity(fromStageEntity);
                    commonJumpDTO.setToStageEntity(toStageEntity);
                    commonJumpDTO.setStageProcessEntity(stageProcessEntity);
                    commonJumpDTO.setStageAttr(stageAttr);
                    commonJumpDTO.setFromApproval(BasicConstant.ONE);
                    commonJumpDTO.setIsNew(false);
                    stageExecute.stageCommonJump(commonJumpDTO);
                } catch (XbbException e) {
                    LOG.error("PaasDataFormDataUpdateServiceImpl.stageCommonJump出错:", e);
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
                }
            }
        }
    }

    @Override
    public void apiHook(FormDataUpdateDTO formDataUpdateDTO, UpdateVO updateVO, BeforeUpdateVO beforeUpdateVO) throws XbbException {
        // 第三方服务
        lowCodeExecuteService(formDataUpdateDTO, updateVO, beforeUpdateVO);
    }

    @Override
    public UpdateVO updateForWorkflow(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, PaasFormEntityExt paasFormEntity, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        try {
            String corpid = formDataUpdateDTO.getCorpid();
            UserVO userVO = userHelp.getUserName(formDataUpdateDTO.getCorpid(), formDataUpdateDTO.getUserId(), formDataUpdateDTO.getLoginUserName(), beforeUpdateVO.getPaasFormDataEntity().getFlowStatus(), beforeUpdateVO.getPaasFormDataEntity().getCreatorId());
            Integer noApprovalRequired = beforeUpdateVO.getNoApprovalRequired();
            Integer updateLine = 0;
            FormDataUtil.removeSystemData(paasFormDataEntity.getData());
            Object richTextData = removeRichText(paasFormDataEntity.getData());
            PaasFormDataEntity formDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(paasFormDataEntity, formDataEntity);
            updateLine = paasFormDataModel.update(formDataEntity);


            //文件柜数据保存
//            saveFile(formDataUpdateDTO, paasFormDataEntity, userVO);
            //富文本数据保存
            Integer updateRichTextLine = updateFormRichText(formDataUpdateDTO, richTextData);
            //表单数据和富文本数据其中有一个编辑就要进工作流
            updateLine = Objects.equals(updateLine, 1) || Objects.equals(updateRichTextLine, 1) ? 1 : 0;
            return new UpdateVO(noApprovalRequired, paasFormDataEntity, updateLine, formDataUpdateDTO.getDataId(), userVO);
        } catch (XbbException e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            LOG.error("purchaseInvoiceFormDataUpdateServiceImpl.update 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public FormDataUpdateVO afterSaveForWorkflow(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, UpdateVO updateVO, PaasFormEntityExt paasFormEntity, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        String corpid = formDataUpdateDTO.getCorpid();
        Long dataId = formDataUpdateDTO.getDataId();
        Integer businessType = formDataUpdateDTO.getBusinessType();
        try {
            FormDataUpdateVO formDataUpdateVO = new FormDataUpdateVO();
            Integer noApprovalRequired = updateVO.getNoApprovalRequired();

            SaasFormSaveDTO saasFormSaveDTO = new SaasFormSaveDTO();
            BeanUtil.copyProperties(formDataUpdateDTO, saasFormSaveDTO);
            packageSaasFormUpdateSaveDTO(saasFormSaveDTO, paasFormDataEntity, beforeUpdateVO.getOldPaasFormData());
            paasFormDataService.afterSaveForUpdateWorkflow(saasFormSaveDTO);
            // 日志
//            afterSaveLog(formDataUpdateDTO, paasFormDataEntity, updateVO, beforeUpdateVO);
            // api发起
//            apiHook(formDataUpdateDTO, updateVO, beforeUpdateVO);

            // 2021/3/3 业务规则
//            activeRule(formDataUpdateDTO, beforeUpdateVO, updateVO);

            formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_RETURN_MSG));
            formDataUpdateVO.setCode(PaasConstant.FORM_ADD_RETURN_CODE);
//            PaasProcessDataEntity paasProcessDataEntity = new PaasProcessDataEntity();
//            paasProcessDataEntity.setCorpid(formDataUpdateDTO.getCorpid());
//            paasProcessDataEntity.setFormDataId(dataId);
//            paasProcessDataEntity.setBusinessType(businessType);
//            paasProcessDataEntity.setSaasMark(formDataUpdateDTO.getSaasMark());
//            proModelService.updateFormDataStatus(paasProcessDataEntity, FlowStatusEnum.IN_APPROVAL.getType());
            formDataUpdateVO.setFormDataId(dataId);
            return formDataUpdateVO;
//        } catch (XbbException e) {
//            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, new ReRollBackEntity());
//            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, new ReRollBackEntity());
            LOG.error("purchaseInvoiceFormDataUpdateServiceImpl.afterSave 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public Long backupData(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, PaasFormEntityExt paasFormEntity, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) {
        String corpid = formDataUpdateDTO.getCorpid();
        Integer isApprove = paasFormEntity.getIsProcessForm();
        if (!formDataUpdateDTO.getIsSatisfyTrigger()) {
            return null;
        }
        JSONObject data = oldPaasFormDataEntity.getData();
        setSystemData(data, oldPaasFormDataEntity);
        long now = DateTimeUtil.getInt();
        data = backupDataOtherInfo(data, corpid, oldPaasFormDataEntity.getId());
        WorkflowBackupDataEntity workflowBackupDataEntity = new WorkflowBackupDataEntity(corpid, oldPaasFormDataEntity.getAppId(), oldPaasFormDataEntity.getFormId(), oldPaasFormDataEntity.getMenuId(), 0L, 0L,
                oldPaasFormDataEntity.getId(), data, now, now, DelEnum.NORMAL.getDel());
        workflowBackupDataModel.insert(workflowBackupDataEntity);
        //富文本数据备份
        backupRichText(workflowBackupDataEntity, formDataUpdateDTO.getBusinessType(), formDataUpdateDTO.getSaasMark());
        return workflowBackupDataEntity.getId();
    }

    @Override
    public JSONObject backupDataOtherInfo(JSONObject data, String corpid, Long dataId) {
        List<PaasUserEntity> paasUserList = paasUserModel.getPaasUserList(corpid, dataId, null);
        data.put(StringConstant.USER_TEAM, paasUserList);
        return data;
    }
}
